<!--
 * @Description: 试题筛选模块
 * @Author: yue
 * @Date: 2022-09-17 14:48:17
 * @LastEditors: yue
 * @LastEditTime: 2022-09-21 11:47:11
-->

<template>
  <div>
    <!-- NOTE：表单 -->
    <el-form ref="form" :model="form" label-width="80px">
      <!-- NOTE：说明操作 -->
      <el-row type="flex" justify="space-between">
        <el-col :span="6">
          <span class="tag">说明：目前支持学科和关键字条件筛选</span>
        </el-col>
        <el-col>
          <el-row type="flex" justify="end">
            <el-button
              type="success"
              size="small"
              icon="el-icon-edit"
              @click="$router.push('/questions/new')"
            >
              新增试题
            </el-button>
          </el-row>
        </el-col>
      </el-row>

      <!-- NOTE：表单部分 -->
      <div class="filter-container">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="学科" prop="subjectID">
              <el-select
                v-model="form.subjectID"
                class="input-width"
                @change="getData"
              >
                <el-option
                  v-for="item in subjectList"
                  :key="item.id"
                  :label="item.subjectName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="二级题目" prop="catalogID">
              <el-select v-model="form.catalogID" class="input-width">
                <el-option
                  v-for="item in directoryList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标签" prop="shortName">
              <el-select v-model="form.tags" class="input-width">
                <el-option
                  v-for="item in tagList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="关键字" prop="state">
              <el-input
                v-model="form.keyword"
                class="input-width"
                placeholder="根据题干搜索"
                @keyup.enter="hQuery"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="试题类型" prop="tags">
              <el-select v-model="form.questionType" class="input-width">
                <el-option
                  v-for="item in [
                    { id: 1, name: '单选' },
                    { id: 2, name: '多选' },
                    { id: 3, name: '简单' }
                  ]"
                  :key="item.name"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="难度" prop="tags">
              <el-select v-model="form.difficulty" class="input-width">
                <el-option
                  v-for="item in [
                    { id: 1, name: '简单' },
                    { id: 2, name: '一般' },
                    { id: 3, name: '困难' }
                  ]"
                  :key="item.name"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向" prop="tags">
              <el-select v-model="form.direction" class="input-width">
                <el-option
                  v-for="item in [
                    'o2o',
                    '外包服务',
                    '企业服务',
                    '互联网金融',
                    '企业咨询',
                    '互联网',
                    '电子商务',
                    '其他'
                  ]"
                  :key="item"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人" prop="tags">
              <el-select v-model="form.creatorID" class="input-width">
                <el-option
                  v-for="item in userList"
                  :key="item.id"
                  :label="item.username"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="题目备注" prop="tags">
              <el-input
                v-model="form.remarks"
                placeholder="请输入"
                class="input-width"
                @keyup.enter="hQuery"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称" prop="tags">
              <el-input
                v-model="form.shortName"
                placeholder="请输入"
                class="input-width"
                @keyup.enter="hQuery"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="城市" prop="tags" class="city">
              <el-select
                v-model="form.province"
                class="filter-item"
                style="width: 125px"
                clearable
                @change="getCity"
              >
                <el-option
                  v-for="item in provinceList"
                  :key="item"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
              <el-select
                v-model="form.city"
                class="filter-item"
                style="width: 125px"
                clearable
              >
                <el-option
                  v-for="item in cityList"
                  :key="item"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-row type="flex" justify="end">
              <el-form-item>
                <el-button size="small" @click="reset">清除</el-button>
                <el-button
                  :loading="isLoading"
                  size="small"
                  type="primary"
                  @click="search"
                >
                  搜索
                </el-button>
              </el-form-item>
            </el-row>
          </el-col>
        </el-row>
      </div>
    </el-form>
  </div>
</template>

<script>
// 请求：省市获取
import { provinces as getProvince, citys as getCity } from '@/api/hmmm/citys.js'
// 请求：学科，目录，标签，用户
import { list as getSubjects } from '@/api/hmmm/subjects.js'
import { simple as getDirectory } from '@/api/hmmm/directorys.js'
import { simple as getTag } from '@/api/hmmm/tags.js'
import { simple as getUserList } from '@/api/base/users.js'

export default {
  data() {
    return {
      userList: [], // 录入人列表
      provinceList: [], // 省列表
      cityList: [], // 市列表
      subjectList: [], // 学科列表
      directoryList: [], // 目录列表
      tagList: [], // 标签列表
      isLoading: false, // 进度条
      // 表单数据
      form: {
        subjectID: null, // 学科
        difficulty: null, // 难度
        questionType: null, // 试题类型
        tags: null, // 标签名称
        province: null, // 省份
        city: null, // 城市
        keyword: null, // 关键字
        remarks: null, // 题目备注
        shortName: null, // 企业简称
        direction: null, // 方向
        creatorID: null, // 录入人
        catalogID: null // 目录
      }
    }
  },
  mounted() {
    // 获取学科
    this.getSubjects()
    // 获取录入人
    this.getUserList()
    // 获取省
    this.getProvince()
  },
  methods: {
    // 获取省
    getProvince() {
      try {
        this.provinceList = getProvince()
        if (this.form.province) {
          this.cityList = getCity(this.form.province)
        }
      } catch (err) {
        this.$message.error(err.message)
      }
    },
    // 获取市
    getCity() {
      try {
        // 改变省份后需要将城市清空
        this.form.city = ''
        this.cityList = getCity(this.form.province)
      } catch (err) {
        this.$message.error(err.message)
      }
    },

    // 获取学科列表
    async getSubjects() {
      try {
        const data = await getSubjects({ page: 1, pagesize: 1000 })
        this.subjectList = data.data.items
      } catch (err) {
        this.$message.error(err.message)
      }
    },
    // 获取目录列表，标签列表
    async getData(subjectID) {
      try {
        // 改变学科需要清空目录，标签
        this.form.catalogID = ''
        this.form.tags = ''
        const directory = await getDirectory({
          page: 1,
          pagesize: 1000,
          subjectID
        })
        const tag = await getTag({ page: 1, pagesize: 1000, subjectID })
        this.directoryList = directory.data
        this.tagList = tag.data
      } catch (err) {
        this.$message.error(err.message)
      }
    },
    // 获取录入人
    async getUserList() {
      try {
        const data = await getUserList({ keyword: this.form.keyword })
        this.userList = data.data
      } catch (err) {
        this.$message.error(err.message)
      }
    },
    // 搜索
    search() {
      const form = {}
      Object.keys(this.form).forEach((item) => {
        if (this.form[item]) {
          form[item] = this.form[item]
        }
      })
      this.$emit('search', form)
    },
    // 重置
    reset() {
      this.form = {
        subjectID: '', // 学科
        difficulty: '', // 难度
        questionType: '', // 试题类型
        tags: '', // 标签名称
        province: '', // 省份
        city: '', // 城市
        keyword: '', // 关键字
        remarks: '', // 题目备注
        shortName: '', // 企业简称
        direction: '', // 方向
        creatorID: '', // 录入人
        catalogID: '' // 目录
      }
      this.directoryList = [] // 目录列表
      this.tagList = [] // 标签列表
    }
  }
}
</script>

<style lang="scss" scoped>
.input-width {
  width: 260px;
}
.tag {
  font-size: 12px;
  color: pink;
}
.filter-container {
  margin-top: 20px;
}
.provAndCity {
  width: 130px;
}
::v-deep .city .el-form-item__content {
  width: 260px;
  .el-select:first-child {
    margin-right: 10px;
  }
}
</style>
